package com.tuanda.app.customviewdemo.paint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.LinearGradient;
import android.graphics.PorterDuff;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.tuanda.app.customviewdemo.BaseCustomView;
import com.tuanda.app.customviewdemo.R;

/**
 * Created by LiDongduo on 2018/9/20.
 */
public class PaintShaderView extends BaseCustomView {
    public PaintShaderView(Context context) {
        super(context);
    }

    public PaintShaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PaintShaderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initData() {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Shader linearShader = new LinearGradient(0,0,80,80, Color.RED,Color.BLUE, Shader
                .TileMode.CLAMP);
        mPaint.setShader(linearShader);
        canvas.drawRect(0,0,80,80,mPaint);

        Shader linearShader1 = new LinearGradient(100,0,100,40, Color.RED,Color.BLUE, Shader
                .TileMode.REPEAT);
        mPaint.setShader(linearShader1);
        canvas.drawRect(100,0,180,80,mPaint);

        Shader radialShader = new RadialGradient(60,170,60, Color.RED,Color.BLUE, Shader
                .TileMode.MIRROR);
        mPaint.setShader(radialShader);
        canvas.drawRect(0,110,120,230,mPaint);

        Shader sweepShader = new SweepGradient(70,290, Color.RED,Color.BLUE);
        mPaint.setShader(sweepShader);
        canvas.drawRect(20,240,120,340,mPaint);

        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        Shader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        mPaint.setShader(bitmapShader);
        canvas.drawRect(140,240,240,340,mPaint);

        Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
        Bitmap logoBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_batman_logo);
        Shader bitmapShader1 = new BitmapShader(sourceBitmap, Shader.TileMode.REPEAT, Shader.TileMode
                .REPEAT);
        Shader bitmapShader2 = new BitmapShader(logoBitmap, Shader.TileMode.MIRROR, Shader.TileMode
                .MIRROR);

        Shader composeShader1 = new ComposeShader(bitmapShader1,bitmapShader2, PorterDuff.Mode
                .SRC_OVER);
        mPaint.setShader(composeShader1);
        canvas.drawCircle(100,450,100,mPaint);

        Shader composeShader2 = new ComposeShader(bitmapShader1,bitmapShader2, PorterDuff.Mode
                .DST_OUT);
        mPaint.setShader(composeShader2);
        canvas.drawCircle(300,450,100,mPaint);

        Shader composeShader3 = new ComposeShader(bitmapShader1,bitmapShader2, PorterDuff.Mode
                .DST_IN);
        mPaint.setShader(composeShader3);
        canvas.drawCircle(500,450,100,mPaint);

        Shader composeShader4 = new ComposeShader(bitmapShader1,bitmapShader2, PorterDuff.Mode
                .ADD);
        mPaint.setShader(composeShader4);
        canvas.drawCircle(330,100,100,mPaint);
    }
}
